﻿Imports System.Activities
Imports System.Data.SqlClient
Imports System.Activities.Presentation.Metadata
Imports System.ComponentModel
Imports System.Drawing

Public Class ExecuteSQLActivity
    Inherits MSSQLBaseActivity

    Public Property SQL As InArgument(Of String)
    Public Property NeedResult As InArgument(Of Boolean)

    Public Sub New()
        Me.DisplayName = My.Resources.ExecuteSQLActivityDisplayName
    End Sub

    Protected Overrides Function BeginExecute(context As System.Activities.AsyncCodeActivityContext, callback As System.AsyncCallback, state As Object) As System.IAsyncResult
        Dim sql = context.GetValue(Me.SQL)
        Dim need = context.GetValue(Me.NeedResult)
        Dim conn = context.GetValue(Me.ConnectionStrings)
        Dim useLog = context.GetValue(Me.OutSQLLog)
        Dim asyncExecute = New asyncExecuteDelegate(AddressOf ExecuteSQL)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(sql, conn, need, useLog, callback, state)
    End Function

    Public Overrides Sub Register()
        MyBase.Register()

        Dim build As New AttributeTableBuilder
        'ExecuteApplicationActivity Activity
        build.AddCustomAttributes(GetType(ExecuteSQLActivity),
                                              New ToolboxBitmapAttribute(GetType(ExecuteSQLActivity), "ExecuteSQL.png"))
        build.AddCustomAttributes(GetType(ExecuteSQLActivity), "SQL",
                                              New RequiredArgumentAttribute,
                                              New CategoryAttribute(My.Resources.CategoryDetail),
                                              New DisplayNameAttribute(My.Resources.SQLDisplay),
                                              New DescriptionAttribute(My.Resources.SQLDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub


End Class
